//
//  ProgramKindViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/7/21.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

//


#import "ProgramKindViewController.h"

#import "ProgramDetailsViewController.h"
#import "ProgramListModel.h"

#import "ProgramKindCell.h"

#import "UIImageView+EMWebCache.h"

@interface ProgramKindViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray * dataSource;

@property (nonatomic, strong) UITableView * kindsTableView;

@end

@implementation ProgramKindViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _dataSource = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //1-食疗养生 2-运动处方 3-营养免疫 4-经络养生 5-身心调养 6-环境养生 7-康复疗养

    [self requestForProgramListData];
    [self creatTableView];
}

- (void)requestForProgramListData
{
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GetWithParameters:nil method:@"get.solution.type" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"解决方案分类的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                ProgramListModel *programListModel = [[ProgramListModel alloc]init];
                [programListModel setValuesForKeysWithDictionary:dataDic];
                [_dataSource addObject:programListModel];
                
            }
            
            [_kindsTableView reloadData];
            _kindsTableView.legendFooter.hidden = YES;

        }
        else
        {
            
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];

        
    }];
    
}



- (void)creatTableView
{
    _kindsTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    
    _kindsTableView.backgroundColor = Lightlightcolor;
    _kindsTableView.delegate = self;
    _kindsTableView.dataSource = self;
    _kindsTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    UINib *nib = [UINib nibWithNibName:@"ProgramKindCell" bundle:nil];
    [_kindsTableView registerNib:nib forCellReuseIdentifier:@"ProKindCell"];
    
    [self.view addSubview:_kindsTableView];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.dataSource count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProKindCell";
    ProgramKindCell *cell = (ProgramKindCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[ProgramKindCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    }
    
    
    //填充数据
    ProgramListModel *model = _dataSource[indexPath.row];
    
    [cell fillCellWithModel:model];
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProgramDetailsViewController *programDetailsViewController = [[ProgramDetailsViewController alloc] init];
    
    programDetailsViewController.solution_id = self.solution_id;
    ProgramListModel *model = _dataSource[indexPath.row];
    programDetailsViewController.type = model.st_id;
    
    programDetailsViewController.title = model.st_name;
    [self.navigationController pushViewController:programDetailsViewController animated:NO];
}



@end
